package com.dsg.util;

import com.dsg.common.util.toolbox.CollectionUtil;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: erp
 * @description:
 * @author: lironglai
 * @create: 2020-07-21 20:56
 */
@Component
public class DsgUtils {

    /**
     * 根据传入的errorList生成StringBuffer
     *
     * @param errorList 错误列表
     * @return StringBuffer
     * @throws IOException
     */
    public StringBuffer genErrorMessage(List errorList) {
        StringBuffer errors = new StringBuffer("");
        for (int j = 0; j < errorList.size(); j++) {
            errors.append((String) errorList.get(j));
            errors.append("<br>");
        }
        return errors;
    }

    /**
     * 生成８位UUID
     *
     * @return
     */
    public static String getUUID() {
        String[] chars = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G",
                "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int j = 0; j < 8; j++) {
            String str = uuid.substring(j * 4, j * 4 + 4);
            int par = Integer.parseInt(str, 16);
            shortBuffer.append(chars[par % 36]);
        }
        return shortBuffer.toString();
    }

    /**
     * 集合分割
     * @param list 集合
     * @param maxNumber 每集合分割数
     * @return
     */
    public static List<List<String>> splitList(List<String> list, Integer maxNumber) {
        if (CollectionUtil.isNotEmpty(list) && maxNumber != null && maxNumber > 0) {
            int limit = (list.size() + maxNumber - 1) / maxNumber;
            List<List<String>> splitList = Stream.iterate(0, n -> n + 1).limit(limit).parallel().map(a -> list.stream().skip(a * maxNumber).limit(maxNumber).parallel().collect(Collectors.toList())).collect(Collectors.toList());
            return splitList;
        } else {
            return null;
        }
    }
}
